
//
//  FunctionDemandTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/28.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "FunctionDemandTabVC.h"
#import "FunctionDemandHeadView.h"
#import "SubjectRecommendModel.h"
#import "HomeModel.h"
#import "DemandCell.h"
@interface FunctionDemandTabVC ()<FunctionDemandHeadViewDelegate,UITableViewDelegate,UITableViewDataSource,DemandCellDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) FunctionDemandHeadView *headView;
//@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,strong) NSString *ass;
@property(nonatomic,strong) NSString *assub;
@property(nonatomic,strong) NSIndexPath *indexPath;
@property(nonatomic,strong) UIButton *checkBtn;
@property(nonatomic,strong) UIButton *contactBtn;
@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;
@end
@implementation FunctionDemandTabVC
-(UITableView *)tableView
{
    if (!_tableView) {
        XLog(@"self.navHeight%ld",self.navHeight);
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.headView.frame)-self.navHeight-44) style:UITableViewStylePlain];
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
        _tableView.mj_header = header;
        self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        [header beginRefreshing];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.view.backgroundColor = [UIColor whiteColor];
    self.headView = [[FunctionDemandHeadView alloc]initWithFrame:CGRectMake(0, 0, kUIScreen.size.width, 50)];
    self.headView.subjectModel = self.subjectModel;
    self.headView.delegate = self;
    [self.view addSubview:self.headView];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectTranslateDemand:) name:@"selectTranslateDemand" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTokenError:) name:@"checkTokenError" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTokenFunctionDemand:) name:@"checkTokenFunctionDemand" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    
    self.pageNum = 1;
    self.pageSize = 10;
    self.ass = self.subjectModel.title;
    self.assub = @"";
//    [self reloadInternetData];
}
-(void)refresh
{
    self.pageNum  = 1;
    self.demandArray = nil;
    [self reloadInternetSelectData];
}
-(void)reloadMore
{
    self.pageNum ++;
    [self reloadInternetSelectData];

}
#pragma mark-代理:网络请求失败
-(void)qingqiusibai:(NSNotification *)info
{
    [self.tableView.mj_header endRefreshing];
}

-(void)selectTranslateDemand:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    
    NSArray *temp = [dict objectForKey:@"date"];
    if (temp.count) {
        for (NSDictionary *dict in temp) {
            HomeModel *model =[HomeModel mj_objectWithKeyValues:dict];
            [self.demandArray addObject:model];
        }
        [self.tableView.mj_footer  endRefreshing];
    }else {
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    if (self.demandArray.count) {
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
        self.footer = footer;
        self.tableView.mj_footer = footer;
                [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
                footer.stateLabel.font = [UIFont systemFontOfSize:12];
                footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        if (!temp.count) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
    }
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
    [WSProgressHUD dismiss];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

-(void)reloadInternetSelectData
{
//    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
//    [WSProgressHUD show];
    NSString *pageNums = [NSString stringWithFormat:@"%ld",(long)self.pageNum];
    NSString *pageSizes = [NSString stringWithFormat:@"%ld",(long)self.pageSize];
    self.dict = @{
                  @"pageNum":pageNums,
                  @"pageSize":pageSizes,
                  @"ass":self.ass,
                  @"assub":self.assub,
                  @"uid":[UserDefaultsHelper readUid],
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:35 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (self.demandArray.count) {
    XLog(@"self.demandArray.count:%ld",self.demandArray.count);
    return  self.demandArray.count;
       
//    }else {
//
//        return 0;
//    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    DemandCell *cell = [DemandCell cellNibWithTableView:tableView];
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.collectShow = NO;
    if (self.demandArray.count) {
        HomeModel *model = self.demandArray[indexPath.row];
        cell.homeModel = model;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    if (indexPath.row == 0) {
    //        return 44;
    //    }else {
    return 144;
    
    //    }
}
#pragma mark-代理:大类选择
- (void)functionDemandHeadView:(FunctionDemandHeadView *)functionDemandHeadView withBtn:(UIButton *)btn
{
    self.demandArray = nil;
    self.assub = [NSString stringWithFormat:@"%@",[btn currentTitle]];
    if ([[btn currentTitle] isEqualToString:@"全部"]) {
        self.assub = @"";
    }
    [self reloadInternetSelectData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     HomeModel *model = self.demandArray[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(functionDemandTabVC:withIndexPath:withModel:)]) {
        [self.delegate functionDemandTabVC:self withIndexPath:indexPath withModel:model];
    }
//    if (self.returnValueBlock) {
//        self.returnValueBlock(model);
//    };
    
}
#pragma mark-代理:显示联系人
-(void)demandCell:(DemandCell *)demandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    self.contactBtn = clickBtn;
    if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]) {
        [LogOutAccount presentOutAccount];
        [UIAlertViewWarningAccount showWarningAccount];
    }else if ([UserDefaultsHelper readAutoLogIn].length == 0) {
        [LogOutAccount presentOutAccount];
    }else {
        self.indexPath = indexPath;
        self.checkBtn = clickBtn;
        self.dict = @{
                      @"userId":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken],
                      };
        [CustomeManager POSTCheckTokenWithURLIndex:3 parameters:self.dict success:^(NSDictionary *dict) {
            
        } failure:^(NSError *error) {
            
        }];
        
    }
    
    
}

-(void)checkTokenFunctionDemand:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSString *message = [info.userInfo objectForKey:@"message"];
    NSInteger status = [[info.userInfo objectForKey:@"status"] integerValue];
    if (status == 200) {
        self.contactBtn.selected = !self.contactBtn.selected;
        HomeModel *model = self.demandArray[self.indexPath.row];
        model.show = self.contactBtn.selected;
        XLog(@"model.show:%d",model.show);
        //    [self.tableView reloadData];
        [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
    }else {
        
    }
    
}
//-(void)qingqiusibai:(NSNotification *)info
//{
//    XLog(@"functionDemand");
//    self.demandArray = nil;
//    [self.tableView reloadData];
//}

-(void)checkTokenError:(NSNotification *)info
{
//    [LogOutAccount presentOutAccount];
//    [UIAlertViewWarningAccount showWarningAccount];
}

@end
